﻿package com.thorqq.magictimer.action;

import com.thorqq.magictimer.R;
import com.thorqq.magictimer.SelectContactsActivity;
import com.thorqq.magictimer.SettingActivityTimer;
import com.thorqq.magictimer.core.ChildView;
import com.thorqq.magictimer.core.ChildViewParent;
import com.thorqq.magictimer.core.TAction;
import com.thorqq.magictimer.timeLoop.TimerMgr;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ActionSendSMSChildView extends ChildView
{
    protected ChildViewParent mParent;
    protected ActionSendSMS mAction;
    protected Context mContext;
    protected LayoutInflater mInflater;
    
    protected EditText mEtPhoneNumber;
    protected EditText mEtContent;
    protected Button   mBtnAddressBook;
        
    public ActionSendSMSChildView(Context context, TAction action)
    {
        mContext = context;
        mAction = (ActionSendSMS) action;
    }

    @Override
    public void updateLayout()
    {
        mEtPhoneNumber.setText(mAction.getPhoneNumberString());
        mEtContent.setText(mAction.getContent());
    }

    @Override
    protected void registerListener()
    {
        mBtnAddressBook.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                Intent intent = new Intent(mContext,SelectContactsActivity.class);
                intent.putExtra(TimerMgr.ALARM_INTENT_ACTION, mAction);
                ((Activity) mContext).startActivityForResult(intent, SettingActivityTimer.REQUEST_CODE_ACTION_SETTING);

            }
        });
        
        mEtPhoneNumber.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void afterTextChanged(Editable arg0)
            {                
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
            {                
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                mAction.setPhoneNumberString(s.toString());
            }
        });
        
        mEtContent.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void afterTextChanged(Editable arg0)
            {                
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
            {                
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                mAction.setContent(s.toString());
            }
        });

    }

    @Override
    protected void doInitLayout(ChildViewParent parent)
    {
        mInflater = LayoutInflater.from(mContext);
        mView = mInflater.inflate(R.layout.action_item_child_sendsms, null);
        mParent = parent;

        mEtPhoneNumber  = (EditText) mView.findViewById(R.id.editTextPhoneNumber);
        mEtContent      = (EditText) mView.findViewById(R.id.editTextContent);
        mBtnAddressBook = (Button) mView.findViewById(R.id.btnAddressBook);
    }
    

    @Override
    protected void doSetTag(Object object)
    {
        if(object instanceof ActionSendSMS)
        {
            mAction = (ActionSendSMS)object;
        }
    }

    @Override
    public void onClose()
    {
        
    }


}
